﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCon;
using System.Data.SqlClient;

namespace yfxt
{
    public partial class cx_bqfy : Form
    {
        public cx_bqfy()
        {
            InitializeComponent();
        }

        public static int ksdm, fyfs;       //传值到打印界面：科室代码、发药方式
        public static string fyrq1, fyrq2;      //传值到打印界面：日期
        string sql_fycx;
        DataTable dt_fycx = new DataTable();
        private void cx_bqfy_Load(object sender, EventArgs e)
        {
            string sqlstr1 = "select ksdm,ksmc from GY_KSDM where BQSY='Y'";
            string sqlstr2 = "select FYFS,FSMC from zy_fyfs";
            //获取数据表数据，将发药方式和病区分别添加到两个combobox中
            DataTable dt1 = new DataTable();
            DataTable dt2 = new DataTable();

            dt1.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlstr1, null));
            dt2.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlstr2, null));

            comboBox1.DataSource = dt2;
            comboBox1.DisplayMember = "fsmc";
            comboBox1.ValueMember = "fyfs";

            comboBox2.DataSource = dt1;
            comboBox2.DisplayMember = "ksmc";
            comboBox2.ValueMember = "ksdm";
        }

        #region 刷新，获取发药药信息
        private void button1_Click(object sender, EventArgs e)
        {
            dt_fycx.Clear();

            #region 获取科室代码

            DataTable dt_ksdm = new DataTable();
            DataTable dt_fyfs = new DataTable();
            string sqlksdm = "select ksdm from gy_ksdm where ksmc='" + comboBox2.Text.ToString().Trim() + "'";
            string sqlfyfs = "select fyfs from zy_fyfs where fsmc='" + comboBox1.Text.ToString().Trim() + "'";
            dt_ksdm.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlksdm, null));
            dt_fyfs.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlfyfs, null));
            ksdm = int.Parse(dt_ksdm.Rows[0]["ksdm"].ToString());
            fyfs = int.Parse(dt_fyfs.Rows[0]["fyfs"].ToString());
            #endregion

            fyrq1 = dateTimePicker1.Value.ToShortDateString();
            fyrq2 = dateTimePicker2.Value.ToShortDateString();

            try
            {
                sql_fycx = "select b.zyh,b.brxm,b.brch,c.ypmc,a.yfgg,a.yfdw,a.fysl,a.ypdj,e.cdmc,(case when(a.fybz=0) then '未发药' when (a.fybz=1) then '已发药' when (a.fybz=2) then '病区退回' end) as fyzt,a.fyrq " +
                "from bq_tj02 a," +
                "zy_brry b," +
                "yk_typk c," +
                "yk_ypcd d," +
                "yk_cddz e," +
                "bq_tj01 f " +
                "where a.fybz=1 " +
                "and a.yfbz='" + mainform.yfsb + "' " +
                "and f.fyfs='" + fyfs + "' " +
                "and f.tjbq='" + ksdm + "' " +
                "and a.tjxh=f.tjxh " +
                "and a.zyh=b.zyh " +
                "and a.ypxh=c.ypxh " +
                "and a.ypxh=d.ypxh " +
                "and d.ypcd=e.ypcd " +
                "and a.fyrq>='" + dateTimePicker1.Value.ToShortDateString() + "' " +
                "and a.fyrq<='" + dateTimePicker2.Value.ToShortDateString() + "' " +
                "order by b.zyh";

                dt_fycx.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sql_fycx, null));
                dataGridView1.Columns[0].DataPropertyName = dt_fycx.Columns[0].ToString();
                dataGridView1.Columns[1].DataPropertyName = dt_fycx.Columns[1].ToString();
                dataGridView1.Columns[2].DataPropertyName = dt_fycx.Columns[2].ToString();
                dataGridView1.Columns[3].DataPropertyName = dt_fycx.Columns[3].ToString();
                dataGridView1.Columns[4].DataPropertyName = dt_fycx.Columns[4].ToString();
                dataGridView1.Columns[5].DataPropertyName = dt_fycx.Columns[5].ToString();
                dataGridView1.Columns[6].DataPropertyName = dt_fycx.Columns[6].ToString();
                dataGridView1.Columns[7].DataPropertyName = dt_fycx.Columns[7].ToString();
                dataGridView1.Columns[8].DataPropertyName = dt_fycx.Columns[8].ToString();
                dataGridView1.Columns[9].DataPropertyName = dt_fycx.Columns[9].ToString();
                dataGridView1.Columns[10].DataPropertyName = dt_fycx.Columns[10].ToString();
                dataGridView1.DataSource = dt_fycx;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion

        #region 打印
        private void button3_Click(object sender, EventArgs e)
        {
            cx_bqfy_crpt crpt = new cx_bqfy_crpt();
            crpt.ShowDialog();
        }
        #endregion

        #region 退出
        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
